package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.dao.UserRoleDao;
import com.example.domain.UserRole;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.UserRoleService;
import com.example.tool.DateTool;

@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    UserRoleDao userRoleDao;

    /**
     * @describe 添加用户角色信息
     */
    @Override
    public Result addUserRole(String json) {
        JSONObject parseObject = JSON.parseObject(json);
        String[] roleId = parseObject.getString("roleId").split(",");
        String[] userId = parseObject.getString("userId").split(",");
        List<UserRole> list = new ArrayList<UserRole>();
        if (userId != null && roleId != null) {
            for (int i = 0; i < userId.length; i++) {
                UserRole userRole = new UserRole();

                userRole.setRoleId(Integer.parseInt(roleId[0]));
                userRole.setUserId(Integer.parseInt(userId[i]));
                // 获取系统当前时间作为记录创建时间
                userRole.setRecordTime(DateTool.getTimestamp());

                list.add(userRole);
            }

        }
        // 调用JPA保存实体方法
        List<UserRole> userRole1 = userRoleDao.saveAll(list);

        if (userRole1 != null) {
            return ResultFactory.buildSuccessResult(userRole1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新用户角色信息
     */
    @Override
    public Result updateUserRole(UserRole userRole) {
        if (userRole != null) {
            // 获取系统当前时间作为记录更新时间
            userRole.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        UserRole userRole1 = userRoleDao.saveAndFlush(userRole);

        if (userRole1 != null) {
            return ResultFactory.buildSuccessResult(userRole1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据用户角色编号删除用户角色信息
     */
    @Override
    public Result deleteUserRoleById(String userRoleId) {
        int row = userRoleDao.deleteByUserRoleId(Integer.parseInt(userRoleId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有用户角色信息
     */
    @Override
    public Result queryAllUserRole() {
        List<UserRole> list = userRoleDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询用户角色信息
     */
    @Override
    public Result queryUserRoleById(String userRoleId) {
        List<UserRole> list = userRoleDao.findByUserRoleId(Integer.parseInt(userRoleId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加用户角色信息
     */
    @Override
    public Result addUserRoleByExcel(List<UserRole> list) {
        List<UserRole> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = userRoleDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据用户角色编号批量删除用户角色信息
     */
    @Override
    public Result deleteUserRoleByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = userRoleDao.deleteByUserRoleIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 用户角色信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;

        if (info[0].equals("userName")) {

            list = userRoleDao.findByUserNameLike(info[1]);

        } else if (info[0].equals("nickName")) {

            list = userRoleDao.findByNickNameLike(info[1]);

        } else if (info[0].equals("roleName")) {

            list = userRoleDao.findByRoleNameLike(info[1]);
        }
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有没有分配角色的用户
     */
    @Override
    public Result queryAllUserOfNotRole() {
        List<Map<String, Object>> list = userRoleDao.queryAllUserOfNotRole();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有角色名称
     */
    @Override
    public Result queryAllRoleName() {
        List<Map<String, Object>> list = userRoleDao.queryAllRoleName();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有用户和角色的已分配信息
     */
    @Override
    public Result queryAllUserAndRoleInfo() {
        List<Map<String, Object>> list = userRoleDao.queryAllUserAndRoleInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
